/*********************************************
 * Student    : Teodoro Cipresso
 * Course     : CS286
 * Professor  : Vidya Setlur
 * Assignment : Term Project (BlueTanks)
 *********************************************/
package edu.sjsu.cs286.teodoro.cipresso.sprites;

import javax.microedition.lcdui.game.Sprite;

/**
 * Instances of this class allow a sprite to move along 16 vectors representing
 * 16 directions through the object and on the playfield.
 * 
 * @author Teodoro Cipresso with lots of help from,
 *         http://developers.sun.com/techtopics/mobility/midp/articles/game/
 */
public class BlueTankVectorTraversal implements IBlueTankSpriteVectorTraversal {

	// X coordinate on vector
	private int vectorX = 0;

	// Y coordinate on vector
	private int vectorY = 0;

	// Sprite traversing vector
	private Sprite sprite = null;

	/**
	 */
	public BlueTankVectorTraversal(Sprite sprite) {
		this.sprite = sprite;
	} // BlueTankVectorTraversal()

	/**
	 * @param direction,
	 *            the vector through the sprite plane that we're traversing. A
	 *            visualization of the vectors is in
	 *            "BlueTankVectorSpriteMove.png".
	 * @param delta,
	 *            the number of units to move along the vector. Negative values
	 *            mean to move backward.
	 */
	public void vectorMove(int direction, int delta) {
		/*
		 * First initialize the vector (X,Y) and adjust if they're not close
		 * enough to the actual x and y.
		 */
		int x = sprite.getX();
		int y = sprite.getY();
		//
		int errorX = Math.abs(vectorX - x * 1000);
		int errorY = Math.abs(vectorY - y * 1000);
		if (errorX > 1000 || errorY > 1000) {
			vectorX = x * 1000;
			vectorY = y * 1000;
		}
		/*
		 * After adjusting the (X,Y) along the vector through the plane, now
		 * compute the desired move
		 */
		int cosX = kCosLookup[direction] * delta;
		int sinY = -kSinLookup[direction] * delta;
		vectorX += cosX;
		vectorY += sinY;
		/*
		 * Set the sprite position
		 */
		sprite.setPosition(vectorX / 1000, vectorY / 1000);
	} // vectorMove()

} // BlueTankVectorTraversal
